package com.yutel.phone.binh.data.xmlhandler;

import java.util.ArrayList;
import java.util.HashMap;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.yutel.phone.binh.data.vo.VideoStream;

public class SAXforVideoStreamHandler extends DefaultHandler {
	private VideoStream videoStream;
	private StringBuffer value;
	private String tag;
	private String temp;

	public VideoStream getVideoStream() {
		return videoStream;
	}
	
	@Override
	public void startDocument() throws SAXException {
		value=new StringBuffer();
	}
	/**
	 * 开始读取内容
	 */
	@Override
	public void startElement(String uri, String localname, String qname,
			Attributes attributes) throws SAXException {
		tag=localname;
		if ("video".equals(tag)) {
			videoStream = new VideoStream();
		} else if ("fileDuration".equals(tag)) {
			videoStream.setFileDuration(new ArrayList<String>());
		} else if ("fileUrl".equals(tag)) {
			videoStream.setFileUrl(new ArrayList<String>());
		} else if ("relative".equals(tag)) {
			videoStream.setRelative(new HashMap<String, String>());
		} else if ("data".equals(tag)) {
			temp=attributes.getValue("version");
		}
	}
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		value.append(ch,start,length);
		String data = new String(ch, start, length).trim();
		if("totalDuration".equals(tag)) {
			double d=Double.valueOf(data)*1000;
			videoStream.setTotalDuration(d);
		} else if("duration".equals(tag)) {
			videoStream.getFileDuration().add(data);
		} else if("file".equals(tag)) {
			videoStream.getFileUrl().add(data);
		} else if("data".equals(tag)) {
			videoStream.getRelative().put(temp, data);
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		String data=value.toString().trim();
		if(data.length()>0) {
			if("totalDuration".equals(tag)) {
				double d=Double.valueOf(data)*1000;
				videoStream.setTotalDuration(d);
			} else if("duration".equals(tag)) {
				videoStream.getFileDuration().add(data);
			} else if("file".equals(tag)) {
				videoStream.getFileUrl().add(data);
			} else if("data".equals(tag)) {
				videoStream.getRelative().put(temp, data);
			}
			value.setLength(0);
		}
	}

}
